//
//  WBMainViewController.swift
//  Swift_weiboDemo
//
//  Created by toro宇 on 2017/5/9.
//  Copyright © 2017年 toro宇. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildControllers()
        createButton()
        // Do any additional setup after loading the view.
    }
    
    // MARK: - 监听方法
    // 撰写微博
    // FIXME: 没有实现
    // @objc 允许这个函数在"运行时"通过 OC 的消息机制被调用
     func clickButton(){
        print("撰写微博")
    }
    
    
    lazy var bottomButton :UIButton = UIButton()
}

// extension 类似于 OC 中 的分类，在 Swift 中还可以用来切分代码块

// 可以把相近功能的函数，房子啊一个 extension 中
// 便于代码维护
// z注意和 OC的分类一样 ，extension 中不能定义属性

// MARK: -设置界面

extension WBMainViewController{
    
    // 设置所有子控制器
    
    // 撰写按钮
        func createButton() {
            
        bottomButton.setBackgroundImage(UIImage(named:"tabbar_compose_button" ), for: .normal)
        bottomButton.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .normal)
        tabBar.addSubview(bottomButton)
        // 计算按钮的宽度
        let  count = CGFloat(childViewControllers.count)
        let  width = tabBar.bounds.size.width / count + 1
            
        bottomButton.frame = tabBar.bounds.insetBy(dx: 2 * width, dy: 0)
//            print("bootomButtonFrame \(bottomButton.frame)")
            bottomButton.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
    }
    
      func  setupChildControllers(){
     
        let vcAry = [
            ["vcName":"WBHomeViewController","title":"首页","imageName":"home"],
            ["vcName":"WBMessageViewController","title":"消息","imageName":"message_center"],
            ["vcName":"UIViewController"],
            ["vcName":"WBDiscoverViewController","title":"发现","imageName":"discover"],
            ["vcName":"WBProfileViewController","title":"我","imageName":"profile"]
            ]
        var controllerAry = [UIViewController]()
        for dict in vcAry {
            controllerAry.append(controller(dic: dict))
        }
        viewControllers = controllerAry

    }
    //[vcName,title,imageName];
    private func controller(dic:[String : String]) -> UIViewController{
        
        // 取得字典内容

        guard let clsName = dic["vcName"],
            let title = dic["title"],
            let imgName = dic["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
        else {
            return UIViewController()
        }
        
        let vc = cls.init()
        vc.title = title
        vc.tabBarItem.image = UIImage(named : "tabbar_" + imgName)
        // 需要渲染一下 withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named:"tabbar_" + imgName + "_selected")?.withRenderingMode(.alwaysOriginal)
        // 修改字体颜色和大小
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.black], for: .highlighted)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 12)], for: .normal)
        let nav = WBNaviViewController.init(rootViewController: vc)
        return nav
    }
    
    
}


